#!/bin/sh -e
# SPDX-License-Identifier: GPL-2.0 or GPL-3.0
# Copyright © 2018-2019 Ariadne Devos

set -e
LC_ALL=C.UTF-8
export LC_ALL

type="$1"
func="$2"
source="$3"


IFS=' '
sizes='2 4 8 16 32 64'

cat <<EOF
#include <sHT/vector.h>
EOF
for size in $sizes; do
cat <<EOF
#if defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
# define sHT_size ${size}
# define sHT_target sHT_target_${size}
# define ${func} ${func}_${size}
# include "${source}"
# undef ${func}
# undef sHT_target
# undef sHT_size
#endif
EOF
done

cat <<EOF
/* musl doesn't allow GNU indirect functions.
   Do the portable thing: save a pointer.  */

#if 0
EOF
for size in $sizes; do
cat <<EOF
#elif defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
typedef __typeof__(&${func}_${size}) ${func}_type;
EOF
done
cat <<EOF
#else
# error impossible
#endif

static const ${func}_type ${func}_variants[] = {
EOF
for size in $sizes; do
cat <<EOF
#if defined(sHT_target_${size}) && (${size} >= sHT_${type}_bytes)
	${func}_${size},
#endif
EOF
done
cat <<EOF
};
EOF
